/*
 * @Author: Snow
 * @Date: 2021-08-30 17:06:35
 * @Description: 
 */
import { WebglBase } from "./webgl/WebglBase";
import { Render } from "./webgl/Render";
import { WebglCube } from "./webgl/WebglCube";
import { WebglMVP } from "./webgl/WebglMVP";
import { Webgl2DBase } from "./webgl/Webgl2DBase";
import { Webgl2DTextureSimple } from "./webgl/Webgl2DTextureSimple";
import { Webgl2DTextureSimple2 } from "./webgl/Webgl2DTextureSimple2";
import { Webgl2DTexture2Image } from "./webgl/Webgl2DTexture2Image";
import { Webgl2DTranslation } from "./webgl/Webgl2DTranslation";
import { Webgl2DMatrix3 } from "./webgl/Webgl2DMatrix3";
import { Webgl3DBase } from "./webgl/Webgl3Dbase";
import { Webgl3DCharF } from "./webgl/Webgl3DCharF";
import { Webgl3DCharFViewProject } from "./webgl/Webgl3DCharFViewProject";
import { Webgl3DCharFPerspective } from "./webgl/Webgl3DCharFPerspective";
import { Webgl3DCharFViewCamara } from "./webgl/Webgl3DCharFViewCamara";
import { Webgl3DCharFDirectionLight } from "./webgl/Webgl3DCharFDirectionLight";
import { Webgl3DCharFPointLight } from "./webgl/Webgl3DCharFPointLight";
import { Webgl3DCharFPointLight2 } from "./webgl/Webgl3DCharFPointLight2";
import { Webgl3DCharFPointLight3 } from "./webgl/Webgl3DCharFPointLight3";
import { TemplateTest } from "./webgl/template/TemplateTest";
import { TemplateDrawMore } from "./webgl/template/TemplateDrawMore";
import { TemplateSunSystem } from "./webgl/template/TemplateSunSystem";
import { TemplateSunSystem2 } from "./webgl/template/TemplateSunSystem2";
import { Texture3DF } from "./webgl/texture/Texture3DF";
import { TextureCube } from "./webgl/texture/TextureCube";
import { TextureDataPix } from "./webgl/texture/TextureDataPix";
import { TemplateProjection } from "./webgl/template/TemplateProjection";
import { TemplateLookCamera } from "./webgl/template/TemplateLookCamera";
import { TemplateProjection2 } from "./webgl/template/TemplateProjection2";
import { TemplateShader1 } from "./webgl/template/TemplateShader1";
import { TemplateShader2 } from "./webgl/template/TemplateShader2";
import { TextureDrawImage1 } from "./webgl/texture/TextureDrawImage1";
import { TextureDrawImage2 } from "./webgl/texture/TextureDrawImage2";
import { TextureDrawImage3 } from "./webgl/texture/TextureDrawImage3";
import { TextureMatrixStack1 } from "./webgl/texture/TextureMatrixStack1";
import { TextureMatrixStack2 } from "./webgl/texture/TextureMatrixStack2";
import { TextureCubeMap } from "./webgl/texture/TextureCubeMap";
import { TextureEnvironmentCubeMap } from "./webgl/texture/TextureEnvironmentCubeMap";
import { TextureSkyBox } from "./webgl/texture/TextureSkyBox";
import { TextureBone1 } from "./webgl/texture/TextureBone1";
import { TextureBoneMatrixTexture } from "./webgl/texture/TextureBoneMatrixTexture";
import { TextureBoneSkin } from "./webgl/texture/TextureBoneSkin";
import { TextureFog } from "./webgl/texture/TextureFog";
import { TemplatePicking } from "./webgl/template/TemplatePicking";
import { TemplateText3D } from "./webgl/template/TemplateText3D";
import { TemplateText3DAtlas } from "./webgl/template/TemplateText3DAtlas";
import { TestSimple } from "./webgl/TestSimple";
import { DrawCallInstance } from "./webgl/skill/DrawCallInstance";
import { HttpRequest } from "./utils/HttpRequest";
import { ClassUtil } from "./ClassUtil";


var configData:JSON;
var Main = async () => {
    ClassUtil.init();
    let instanceClass: any;
    //实例化渲染
    //instanceClass = DrawCallInstance;
    //简单测试
    //instanceClass = TestSimple;
    //const json = await loadJson("config.json");
    //console.log(json);

    console.log(configData);
    
    var urlObj = {};
    let url: string = location.search;
    url = url.substr(1);
    let arr = url.split("&");
    for (let index = 0; index < arr.length; index++) {
        const element = arr[index];
        if (element) {
            let st = element.split("=");
            if (st.length > 1) {
                urlObj[st[0]] = st[1];
            }
        }
    }

    let id = urlObj["example"];
    id = id || 1;

    let list = (configData as any).examples;
    for (let index = 0; index < list.length; index++) {
        const element = list[index];
        if(id == element.id){
            let name = element.class;
            let debug = element.debug;
            instanceClass = ClassUtil.getClass(name);
            instanceClass && (instanceClass.debug = debug);
            break;
        }
    }
    

    


    //可视化相机 
    //instanceClass = TemplateLookCamera;
    
    let startWebgl = () => {
        const canvas: any = document.querySelector("#canvas");
        const gl: WebGLRenderingContext = canvas.getContext("webgl")
        Render.GL = gl;
        Render.Canvas = canvas;
        //gl.viewport(0,0,Render.clientWidth,Render.clientHeight);
        window.addEventListener("resize", (): any => {
            // console.log("width = " + gl.canvas.width)
            // console.log("height = " + gl.canvas.height)
            // console.log("clientWidth = " + (gl.canvas as HTMLCanvasElement).clientWidth)
            // console.log("clientHeight = " + (gl.canvas as HTMLCanvasElement).clientHeight)
        })
        console.log("canvas szie info", "width = " + gl.canvas.width)
        console.log("width = " + gl.canvas.width)
        console.log("height = " + gl.canvas.height)
        console.log("clientWidth = " + (gl.canvas as HTMLCanvasElement).clientWidth)
        console.log("clientHeight = " + (gl.canvas as HTMLCanvasElement).clientHeight)

        if (!gl) {
            alert("无法初始化WEBGL")
            return;
        }
        

        //console.log(urlObj);

        let webGl;
        //webGl = new Webgl2DBase();

        //纹理投影
        //webGl = new TemplateProjection();

        //纹理投影 模拟放影机的投影
        //webGl = new TemplateProjection2();

        //阴影  聚光灯
        //webGl = new TemplateShader1();

        //阴影  方向光源
        //webGl = new TemplateShader2();

        //可视化相机 
        //webGl = new TemplateLookCamera();

        //绘制纹理 
        //webGl = new TextureDrawImage3();

        //矩阵栈 
        //webGl = new TextureMatrixStack2();

        //立方体贴图
        //webGl = new TextureCubeMap();

        //环境贴图
        //webGl = new TextureEnvironmentCubeMap();

        //天空盒
        //webGl = new TextureSkyBox();

        //骨骼
        //webGl = new TextureBone1();

        //骨骼  纹理寸矩阵数据
        //webGl = new TextureBoneMatrixTexture();

        //骨骼  鲸鱼蒙皮
        //webGl = new TextureBoneSkin();

        //  雾
        //webGl = new TextureFog();

        // 选中物品
        //webGl = new TemplatePicking();

        // 文本纹理 有点问题没解决
        //webGl = new TemplateText3D();

        // 文字图集
        //webGl = new TemplateText3DAtlas();

        // 简单测试
        //webGl = new TestSimple();

        //实例化渲染  
        webGl = new instanceClass();
        
        let render = (ts) => {
            webGl.render(ts / 1000);
            requestAnimationFrame(render);
        }
        requestAnimationFrame(render);
    }

    let loadLib = (url:string)=> {
        var script = document.createElement("script");
        script.async = false;
        script.src = url;
        document.body.appendChild(script);
    }

    if (instanceClass.debug) {
        window.addEventListener('gliready', ()=>{
            startWebgl();
        });
        (window as any).gliEmbedDebug = true;
        loadLib("./core/embed.js");
    }else{
        startWebgl();
    }
}

async function loadJson(url:string,typeFunc:string = "json") {
    const response = await fetch(url);
    if (!response.ok) {
        throw new Error(`could not load: ${url}`);
    }
    return await response[typeFunc]();
}

// (window as any)._startFun = Main;
// Main();
let httpRequest = new HttpRequest();
httpRequest.send("config.json",null,null,"json");
httpRequest.onComplete = (data:any):void=>{
    console.log("配置加载成功");
    configData = data;
    (window as any)._startFun = Main;
    Main();
};


